package com.cci.kangdao.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.dao.SymptomCategoryTDao;
import com.cci.kangdao.dao.SymptomInfoTDao;
import com.cci.kangdao.model.WorkOrderSymptomsDataModel;
import com.cci.kangdao.model.condition.GetSymptomMsgListCondition;
import com.cci.kangdao.service.SymptomInfoService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.ParameterTool;

@Service("SymptomInfoService")
public class SymptomInfoServiceImpl implements SymptomInfoService {

	private Logger log = Logger.getLogger(this.getClass().getName());
	@Resource
	private SymptomCategoryTDao symptomCategoryTDao;
	@Resource
	private SymptomInfoTDao symptomInfoTDao;

	//获取症状集合
	public ModelAndView getSymptomList(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse) map.get("response");
		JSONObject obj = new JSONObject();  //声明返给前端json对象
		String keyWord = MapUtils.getString(map,"KeyWord","");
		try {
			List<Map<String,Object>> symptomCategoryList = symptomCategoryTDao.getSymptomCategoryList();
			List<Map<String,Object>> newSymptomCategoryList = new ArrayList<>();
			if(CollectionUtils.isNotEmpty(symptomCategoryList)){
				for(Map<String,Object> symptomCategoryMap : symptomCategoryList){
					String categoryId = MapUtils.getString(symptomCategoryMap,"CategoryId","");
					List<Map<String,Object>> symptomInfoList = new ArrayList<>();
					if(StringUtils.isNotEmpty(categoryId)){
						symptomInfoList = symptomInfoTDao.getSymptomInfoListByCategoryId(categoryId,keyWord);
					}
					symptomCategoryMap.put("SymptomList",symptomInfoList);
					if(CollectionUtils.isNotEmpty(symptomInfoList)){
						newSymptomCategoryList.add(symptomCategoryMap);
					}
				}
			}
			Map<String,Object> symptomCategoryNameListMap = new HashMap<>();
			symptomCategoryNameListMap.put("SymptomCategoryNameList",newSymptomCategoryList);
			obj.put("data", symptomCategoryNameListMap);
			obj.put("status", 0);
			obj.put("msg", "success");
			ParameterTool.writeResponse(response, obj.toString());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
			// 主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return null;
	}

	@Override
	public List<WorkOrderSymptomsDataModel> getSymptomMsgList(GetSymptomMsgListCondition condition) {
		List<WorkOrderSymptomsDataModel> symptomMsgList = symptomInfoTDao.getSymptomMsgList(condition);
		if(CollectionUtils.isEmpty(symptomMsgList)) {
			// 当查询的数据为空时，查询出默认的数据
			symptomMsgList = symptomInfoTDao.getSymptomMsgDefaultList(condition);
		}
		return symptomMsgList;
	}

	


}
